package com.example.securityconfig.config;

import com.alibaba.fastjson.JSON;

import com.example.securityconfig.entity.Result;
import com.example.securityconfig.entity.enums.ExceptionCodeEnum;
import com.example.securityconfig.util.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
* 认证失败处理器
* */
@Component
public class SecurityAuthenticationEntryPointExceptionHandler implements AuthenticationEntryPoint {
 
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        //  SECURITY_CODE(500205, "用户认证失败");
        Result<Object> error = Result.error(ExceptionCodeEnum.SECURITY_ERROR);
        String s = JSON.toJSONString(error);
        // 处理异常
        WebUtils.renderString(response, s);
    }
}